<?php
#### PAGINATION ################################################################
class Pagination extends Base {

	var $count;
	var $url;
	var $per_page;
	var $current_page;
	var $output;
	var $pages;

	#### CONSTRUCTOR ###########################################################
	function Pagination() {
		Base::getThis();
	}

	#### SET COUNT #############################################################
	function setCount($integer) {
		$this->count = $integer;
	}

	#### SET COUNT #############################################################
	function setUrl($string) {
		$this->url = $string;
	}

	#### SET COUNT #############################################################
	function setPerPage($integer) {
		$this->per_page = $integer;
	}

	#### SET COUNT #############################################################
	function setCurrentPage($integer) {
		$this->current_page = $integer;
	}

	#### LIMIT #################################################################
	// Generates a limit array to be used in your SQL
	function limit() {

		$limit = NULL;

		$page = $this->current_page - 1; // Change page to match array as arrays start from 0 but pages from 1
		if ($this->per_page) {
			$limit = array(
				$page * $this->per_page, // Start From
				$this->per_page // Limit
			);
		}

		return $limit;
	}

	#### SET CHECK #############################################################
	function setCheck() {
		if (!isset($this->count)) $this->output .= 'Please set a count integer via setCount()<br />';
		if (!isset($this->url)) $this->url = url('all');
		if (!isset($this->per_page)) $this->output .= 'Please set a per_page integer via setPerPage()<br />';
		if (!isset($this->current_page)) $this->output .= 'Please set a current_page integer via setCurrentPage()<br />';

		if ($this->output) return TRUE;
	}

	#### BUILD PAGINATION ######################################################
	// Returns the output of the pagination call
	// NOTE: Nothing will be returned if there is not more than one page
	function buildPagination($alignment = 'left') {

		if($this->setCheck()) return $this->output;

		$page = '/page-';

		if (!$this->per_page) return;
		$total_pages = ceil($this->count / $this->per_page);

		if ($total_pages > 1) {
			$this->pages = '<b>Page:</b> ';

			// Previous link
	   		if ($this->current_page > 1) $this->pages .= ' <a href="'.$this->url.$page.($this->current_page - 1).'" title="Page '.($this->current_page - 1).'">'.$this->icon['previous'].'</a> ';
			else $this->pages .= '&nbsp;&nbsp;&nbsp;&nbsp;';

			// If there are more then 10 pages, some pages need to be skipped
			if ($total_pages > 10) {

				// First 3 pages
				$init_page_max = ($total_pages > 3 ? 3 : $total_pages);
				for($i = 1; $i < $init_page_max + 1; $i++)
				{
					$this->pages .= ($i == $this->current_page ? '<span class="selected">'.$i.'</span>' : '<a href="'.$this->url.$page.$i.'" title="Page '.$i.'">'.$i.'</a>');
					if ($i < $total_pages) $this->pages .= ' ';
				}

				// Centre pages
				if ($this->current_page > 1 && $this->current_page < $total_pages) {
					if ($this->current_page > 5) $this->pages .= ' ... ';
					$init_page_min = ($this->current_page > 4 ? $this->current_page : 5);
					$init_page_max = ($this->current_page < $total_pages - 4 ? $this->current_page : $total_pages - 4);

					for($i = $init_page_min - 1; $i < $init_page_max + 2; $i++)
					{
						$this->pages .= ($i == $this->current_page ? '<span class="selected">'.$i.'</span>' : '<a href="'.$this->url.$page.$i.'" title="Page '.$i.'">'.$i.'</a>');
						if ($i < $total_pages) $this->pages .= ' ';
					}
					$this->pages .= ($current_page < $total_pages - 4 ? ' ... ' : ' ');
				} else {
					$this->pages .= ' ... ';
				}

				// Last 3 pages
				for($i = $total_pages - 2; $i < $total_pages + 1; $i++)
				{
					$this->pages .= ($i == $this->current_page ? '<span class="selected">'.$i.'</span>' : '<a href="'.$this->url.$page.$i.'" title="Page '.$i.'">'.$i.'</a>');
					if ($i < $total_pages) $this->pages .= ' ';
				}
			} else {
				// Less then 10 pages, so return all
				for($i = 1; $i < $total_pages + 1; $i++)
				{
					$this->pages .= ($i == $this->current_page ? '<span class="selected">'.$i.'</span>' : '<a href="'.$this->url.$page.$i.'" title="Page '.$i.'">'.$i.'</a>');
					if ($i < $total_pages) $this->pages .= ' ';
				}
			}

			// Next links
			if ($this->current_page < $total_pages) $this->pages .= ' <a href="'.$this->url.$page.($this->current_page + 1).'" title="Page '.($this->current_page + 1).'">'.$this->icon['next'].'</a>';
		}

		if ($alignment == 'left') $this->output = '<div class="pagination" style="text-align:left">'.$this->pages.'</div>';
		else if ($alignment == 'right') $this->output = '<div class="pagination" style="text-align:right">'.$this->pages.'</div>';
		else $this->output = '<div class="pagination" style="text-align:center">'.$this->pages.'</div>';

		return $this->output;
	}

	#### OUTPUT ################################################################
	function output() {
		return $this->output;
	}

}
?>